package smartt.styy.auth.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import smartt.styy.auth.biz.TypeRelationBiz;
import smartt.styy.auth.biz.UserBiz;
import smartt.styy.auth.constants.RedisConstants;
import smartt.styy.auth.constants.ServerConstants;
import smartt.styy.auth.model.bean.UserInfoVo;
import smartt.styy.auth.model.entity.TbAuUser;
import smartt.styy.auth.model.entity.TbTypeRelation;
import smartt.styy.auth.model.jwt.JWTInfo;
import smartt.styy.auth.model.request.LoginVo;
import smartt.styy.auth.model.request.Manager4Password;
import smartt.styy.auth.model.request.RegisterInfoReq;
import smartt.styy.auth.model.request.TelPhoneLoginReq;
import smartt.styy.auth.model.request.ThirdLoginReq;
import smartt.styy.auth.model.request.ThirdRegisterReq;
import smartt.styy.auth.model.request.UserTermDeviceEntity;
import smartt.styy.auth.model.response.AnonymousRsp;
import smartt.styy.auth.model.response.PhoneUserLoginRsp;
import smartt.styy.auth.model.response.RegisterInfoRsp;
import smartt.styy.auth.model.response.ThridLoginRsp;
import smartt.styy.auth.model.utils.JwtTokenUtil;
import smartt.styy.auth.service.UserService;
import smartt.styy.auth.service.VcodeManager;
import smartt.styy.auth.service.VerificationCodeService;
import smartt.styy.auth.util.DESEncrypt;
import smartt.styy.auth.util.IdGenerator;
import smartt.styy.auth.util.MD5Util;
import smartt.styy.auth.util.PwdUtils;
import smartt.styy.auth.util.RedisCacheUtil;
import smartt.styy.common.bean.AnonymousInfo;
import smartt.styy.common.constant.RestCodeConstants;
import smartt.styy.common.constant.UserConstant;
import smartt.styy.common.exception.BaseException;
import smartt.styy.common.msg.ObjectRestResponse;

@RestController
@RequestMapping("registerLogin")
@Slf4j
@Api(value="RegisterController",tags="[用户注册+多种登录方式]")
public class RegisterController {

	@Autowired
	private UserBiz userBiz;
	
	@Autowired
	private TypeRelationBiz typeRelationBiz;
	
	@Autowired
	private VcodeManager vcodeManager;
	
	@Autowired
	private VerificationCodeService verificationCodeService;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private JwtTokenUtil jwtTokenUtil;
	
	@Autowired
	private IdGenerator idGenerator;
	
	@Autowired
	private RedisCacheUtil redisCacheUtil;
	
	@Value("${3des.key}")
	private String desKey;
	
	/**
	 * Description:用户注册 <br>
	 * @author stf<br>
	 * @param UserInfoReq <br>
	 * @return String <br>
	 * @throws BaseException <br>
	 * */
	@SuppressWarnings("unchecked")
	@PostMapping("/postUser")
	@ApiOperation(value="[用户注册]-[用户账号+密码注册提交]", notes="用户账号，密码注册提交")
	@ApiImplicitParam(name = "param", value = "用户注册param", required = true, dataType = "RegisterInfoReq")
	public ObjectRestResponse<Object> postUser(@RequestBody RegisterInfoReq param){
		log.info("[用户注册]-[用户账号+密码提交] start ...");
		ObjectRestResponse<Object> objectRestResp = new ObjectRestResponse<Object>();
		
		String password = param.getPassword();
		String username = param.getUsername();
		Integer userType = param.getUserType();
		
		if (StringUtils.isAnyBlank(username, password) || null ==userType) {
			log.error("[用户注册]-[用户账号+密码提交]--参数为空！");
			objectRestResp.setStatus(UserConstant.USER_PHONE_REGISTER_CODE);
			objectRestResp.setMessage(UserConstant.USER_PHONE_REGISTER_MSG);
			objectRestResp.setRel(false);
			return objectRestResp;
		}
		
		
		RegisterInfoRsp regInfoRsp = new RegisterInfoRsp();
		TbAuUser auUser = new TbAuUser();
		try {
			
			//step1:判断用户是否存在
			Map<String,Object> reqMap = new HashMap<String,Object>();
			reqMap.put("username", param.getUsername());
			reqMap.put("userType", param.getUserType());
			List<UserInfoVo> userVo = userBiz.getUserInfoQuery(reqMap);
			if(userVo.size() >0){
				log.warn("[用户注册]-[用户账号+密码提交] 用户："+param.getUsername()+"存在，请修改后再提交！");
				objectRestResp.setStatus(UserConstant.USER_EXIST_CODE);
				objectRestResp.setMessage(UserConstant.USER_EXIST_MSG);
				objectRestResp.setRel(false);
				return objectRestResp;
			}
			
			//判断父账号是否存在
			if(param.getParentId()!= null && param.getParentId() !=ServerConstants.USER_Id_ZERO){
				if(null != userBiz.selectById(param.getParentId())){
					auUser.setParentId(param.getParentId());
				}else{
					log.error("[用户注册]-[用户账号+密码提交] 用户："+param.getUsername()+"父账号不存在，请修改后再提交！");
					objectRestResp.setStatus(UserConstant.USER_NOT_EXIST_CODE);
					objectRestResp.setMessage(UserConstant.USER_NOT_EXIST_MSG);
					objectRestResp.setRel(false);
					return objectRestResp;
				}
			}
			
			TbTypeRelation trEntity = new TbTypeRelation();
			auUser.setUsername(param.getUsername());
			String dbPassword = PwdUtils.generateDbPwd(param.getUsername(),param.getPassword());
			auUser.setPassword(MD5Util.encrypt(dbPassword));
			userBiz.insertSelective(auUser);
			Integer userId = auUser.getId();
			trEntity.setUserId(userId);
			trEntity.setUsertypeId(param.getUserType());
			typeRelationBiz.insertSelective(trEntity);
			
			//构建返回对象
			regInfoRsp.setUserId(DESEncrypt.encryptMode(desKey,userId.toString()));
			regInfoRsp.setUsername(param.getUsername());
			regInfoRsp.setStatus(ServerConstants.USER_STATUS_1);
			regInfoRsp.setUserType(param.getUserType());
			if(null !=param.getParentId()){
				regInfoRsp.setParentId(DESEncrypt.encryptMode(desKey,param.getParentId().toString()));
			}
			//注册返回token
			String token = jwtTokenUtil.generateToken(new JWTInfo(regInfoRsp.getUsername(), userId + "", "",param.getUserType()+"", ""));
			regInfoRsp.setToken(token);
			
		} catch (Exception e) {
			log.error("[用户注册]-[用户账号+密码提交]-异常，错误message:"+e.getMessage());
			throw new BaseException("[用户注册]-[用户账号+密码提交]-异常，错误message:"+e.getMessage(),RestCodeConstants.THROW_ERROR_CODE);
		}
		log.info("[用户注册]-[用户注册post提交] end 。");
		return objectRestResp.data(regInfoRsp).rel(true);
		
	}
	
	
	/**
	 * Description:手机号码注册 <br>
	 * @author stf<br>
	 * @param UserInfoReq <br>
	 * @return ObjectRestResponse <br>
	 * @throws BaseException <br>
	 * */
	@SuppressWarnings("unchecked")
	@ApiOperation(value = "手机号注册", notes = "手机号注册")
	@PostMapping(value = "/reg_by_mobile")
	@ApiImplicitParam(name = "param", value = "手机号注册param", required = true, dataType = "TelPhoneLoginReq")
	public ObjectRestResponse<Object> registerByCellphoneNumber(@RequestBody TelPhoneLoginReq param) {
		log.info("[用户注册]-[手机号码注册] start...");
		ObjectRestResponse<Object> objectRestResp = new ObjectRestResponse<Object>();
		RegisterInfoRsp regInfoRsp = new RegisterInfoRsp();
		
		String mobile = param.getMobile();
		String passwd = param.getPasswd();
		String smsCode = param.getSmsCode();
		Integer userType = param.getUserType();
		String ipAddr = param.getIp();
		
		//验证参数
		if (StringUtils.isAnyBlank(mobile, passwd, smsCode) || null ==userType) {
			log.error("[用户注册]-[手机号码注册]--参数为空！");
			objectRestResp.setStatus(UserConstant.USER_PHONE_REGISTER_CODE);
			objectRestResp.setMessage(UserConstant.USER_PHONE_REGISTER_MSG);
			objectRestResp.setRel(false);
			return objectRestResp;
		}
		
		//手机号码格式校验
		if(!Pattern.matches(ServerConstants.REGMOBILE, mobile)) {
			log.error("[用户注册]-[手机号码注册]--电话号码不正确");
			objectRestResp.setStatus(UserConstant.USER_PHONE_REGISTER_50710);
			objectRestResp.setMessage(UserConstant.USER_PHONE_REGISTER_50710_MSG);
			objectRestResp.setRel(false);
			return objectRestResp;
		}
		
		//校验手机号码是否已被注册
		Map<String,Object> reqMap =new HashMap<String,Object>();
		reqMap.put("status", ServerConstants.USER_STATUS_1);
		reqMap.put("telPhone", mobile);
		TbAuUser tbUser = userBiz.selectUserInfoByParams(reqMap);
		if(tbUser != null){
			log.warn("[用户注册]-[手机号码注册]--电话号码已存在！");
			objectRestResp.setStatus(UserConstant.USER_PHONE_CODE);
			objectRestResp.setMessage(UserConstant.USER_PHONE_MSG);
			objectRestResp.setRel(false);
			return objectRestResp;
		}
		
		//短信验证码校验
		Object redisPhoneCode = vcodeManager.getVcode(RedisConstants.REDIS_REGISTER_PREFIX,mobile);
		if(null != redisPhoneCode){
			if(!smsCode.equals(redisPhoneCode.toString())){
				log.warn("[用户注册]-[手机号码注册]--短信验证码不正确！");
				objectRestResp.setStatus(UserConstant.USER_VCODE_CODE);
				objectRestResp.setMessage(UserConstant.USER_VCODE_MSG);
				objectRestResp.setRel(false);
				return objectRestResp;
			}
		}else{
			log.warn("[用户注册]-[手机号码注册]--未在缓冲中获取到手机号："+mobile+"短信验证码！");
			objectRestResp.setStatus(UserConstant.USER_VCODE_CODE);
			objectRestResp.setMessage(UserConstant.USER_VCODE_MSG);
			objectRestResp.setRel(false);
			return objectRestResp;
		}
		
		try {
			TbAuUser tbAuUser = new TbAuUser();
			tbAuUser.setUsername(mobile);
			String dbPassword = PwdUtils.generateDbPwd(mobile,passwd);
			tbAuUser.setPassword(MD5Util.encrypt(dbPassword));
			tbAuUser.setTelPhone(mobile);
			tbAuUser.setNickName(idGenerator.nextId());
			tbAuUser.setIpAddr(ipAddr);
			userBiz.insertSelective(tbAuUser);
			Integer userId = tbAuUser.getId();
			
			TbTypeRelation tbTypeRel = new TbTypeRelation();
			tbTypeRel.setUserId(userId);
			tbTypeRel.setUsertypeId(userType);
			typeRelationBiz.insertSelective(tbTypeRel);
			
			//构建返回对象
			regInfoRsp.setUserId(DESEncrypt.encryptMode(desKey,userId.toString()));
			regInfoRsp.setUsername(tbAuUser.getUsername());
			regInfoRsp.setStatus(ServerConstants.USER_STATUS_1);
			regInfoRsp.setUserType(param.getUserType());
			regInfoRsp.setTelPhone(mobile);
			//注册返回token
			String token = jwtTokenUtil.generateToken(new JWTInfo(regInfoRsp.getUsername(), userId.toString() + "", "",param.getUserType()+"",mobile));
			regInfoRsp.setToken(token);
			
		} catch (Exception e) {
			log.error("[用户注册]-[手机号码注册]--数据插入失败！");
			throw new BaseException("[用户注册]-[手机号码注册]--数据插入失败-异常，错误message:"+e.getMessage(),RestCodeConstants.THROW_ERROR_CODE);
		}
		log.info("[用户注册]-[手机号码注册] end 。");
		return objectRestResp.data(regInfoRsp).rel(true);
		
	}
	
	/**
	 * Description:第三方登录 <br>
	 * @author stf<br>
	 * @param UserInfoReq <br>
	 * @return ObjectRestResponse <br>
	 * @throws BaseException <br>
	 * */
	@ApiOperation(value = "第三方登录", notes = "第三方登录")
	@PostMapping(value = "/login_by_third_acc")
	@ApiImplicitParam(name = "param", value = "第三方登录param", required = true, dataType = "ThirdLoginReq")
	public ObjectRestResponse<Object> loginByThirdAppAccount(@RequestBody ThirdLoginReq param) {
		log.info("[用户注册]-[第三方登录] start...");
		
		ObjectRestResponse<Object> objectRestResp = new ObjectRestResponse<Object>();
		String thridAccount = param.getThridAccount();
		Integer thridAccountType = param.getThridAccountType();
		Integer userType = param.getUserType();
		
		if (StringUtils.isAnyBlank(thridAccount)) {
			log.error("[用户注册+多方式登录]-[第三方登录]--参数为空！");
			objectRestResp.setStatus(UserConstant.USER_PHONE_50810);
			objectRestResp.setMessage(UserConstant.USER_PHONE_50810_MSG);
			objectRestResp.setRel(false);
			return objectRestResp;
		}
		
		if(thridAccountType == null || userType == null){
			log.error("[用户注册+多方式登录]-[第三方登录]--参数为空！");
			objectRestResp.setStatus(UserConstant.USER_PHONE_50810);
			objectRestResp.setMessage(UserConstant.USER_PHONE_50810_MSG);
			objectRestResp.setRel(false);
			return objectRestResp;
		}
		
		ThridLoginRsp accVo = new ThridLoginRsp();
		try {
			accVo =userService.loginByThirdAppAccount(param);
			
			//封装返回数据
			objectRestResp.setRel(true);
			objectRestResp.setStatus(RestCodeConstants.SUCCESS_CODE);
			if(null == accVo.getUserId()){
				objectRestResp.setMessage("第三方登录，未返回token，还需要进行第三方登录第二步操作！");
			}
			objectRestResp.setData(accVo);
			return objectRestResp;
		} catch (Exception e) {
			log.error("[用户注册]-[第三方登录]--第三方登录失败！");
			throw new BaseException("[用户注册]-[第三方登录]--第三方登录失败，错误message:"+e.getMessage(),RestCodeConstants.THROW_ERROR_CODE);
		}
	}
	
	/**
	 * Description:第三方登录第二步 <br>
	 * @author stf<br>
	 * @param UserInfoReq <br>
	 * @return ObjectRestResponse <br>
	 * @throws BaseException <br>
	 * */
	@ApiOperation(value = "第三方登录第二步", notes = "第三方登录第二步,login_by_third_acc未返回token时调用")
	@PostMapping(value = "/register_by_third_acc")
	@ApiImplicitParam(name = "param", value = "第三方登录第二步param", required = true, dataType = "ThirdRegisterReq")
	public ObjectRestResponse<Object> updateAccountInfoFromThirdAPPAndBindingCellphone(@RequestBody ThirdRegisterReq param) {
		log.info("[用户注册]-[第三方登录第二步] start...");
		
		ObjectRestResponse<Object> objectRestResp = new ObjectRestResponse<Object>();
		RegisterInfoRsp regInfoRsp = new RegisterInfoRsp();
		try {
			String mobile = param.getMobile();
			String smsCode = param.getSmsCode();
			Integer thridId = param.getThridId();
			if (StringUtils.isAnyBlank(mobile,smsCode)) {
				objectRestResp.setStatus(UserConstant.USER_PHONE_REGISTER_CODE);
				objectRestResp.setMessage(UserConstant.USER_PHONE_REGISTER_MSG);
				objectRestResp.setRel(false);
				return objectRestResp;
			}
			
			//手机号码格式校验
			if(!Pattern.matches(ServerConstants.REGMOBILE, mobile)) {
				log.error("[用户注册]-[第三方登录第二步]--电话号码不正确");
				objectRestResp.setStatus(ServerConstants.ERROR_60050);
				objectRestResp.setMessage(ServerConstants.ERROR_MSG_60050);
				objectRestResp.setRel(false);
				return objectRestResp;
			}
			
			//短信验证码校验
			Object redisPhoneCode = vcodeManager.getVcode(RedisConstants.REDIS_ACCOUNT_PREFIX,mobile);
			if(null != redisPhoneCode){
				if(!smsCode.equals(redisPhoneCode.toString())){
					log.warn("[用户注册]-[第三方登录第二步]--短信验证码不正确！");
					objectRestResp.setStatus(UserConstant.USER_VCODE_CODE);
					objectRestResp.setMessage(UserConstant.USER_VCODE_MSG);
					objectRestResp.setRel(false);
					return objectRestResp;
				}
			}
			
			if(thridId ==null||thridId ==ServerConstants.USER_Id_ZERO || param.getUserType() == null){
				log.warn("[用户注册]-[第三方登录第二步]--thridId传入错误！");
				objectRestResp.setStatus(UserConstant.USER_PHONE_50810);
				objectRestResp.setMessage(UserConstant.USER_PHONE_50810_MSG);
				objectRestResp.setRel(false);
				return objectRestResp;
			}
			
			Integer userId =userService.updateAccountInfoFromThirdAPP(param);
			
			//构建返回对象
			regInfoRsp.setUserId(DESEncrypt.encryptMode(desKey,userId.toString()));
			regInfoRsp.setUsername(mobile);
			regInfoRsp.setStatus(ServerConstants.USER_STATUS_1);
			regInfoRsp.setUserType(param.getUserType());
			regInfoRsp.setTelPhone(mobile);
			//注册返回token
			String token = jwtTokenUtil.generateToken(new JWTInfo(regInfoRsp.getUsername(), 
					userId.toString() + "", "",param.getUserType()+"",mobile));
			regInfoRsp.setToken(token);
			
		} catch (Exception e) {
			log.error("[用户注册]-[第三方登录第二步]--第三方登录第二步失败！");
			throw new BaseException("[用户注册]-[第三方登录第二步]--数据插入失败-异常，错误message:"+e.getMessage(),
					RestCodeConstants.THROW_ERROR_CODE);
		}
		
		log.info("[用户注册]-[第三方登录第二步] end。");
		objectRestResp.setRel(true);
		objectRestResp.setStatus(RestCodeConstants.SUCCESS_CODE);
		objectRestResp.setMessage("第三方登录第二步...success!");
		objectRestResp.setData(regInfoRsp);
		return objectRestResp;
	}
	
	/**
	 * Description:匿名登录 <br>
	 * @author stf<br>
	 * @param UserTermDeviceEntity <br>
	 * @return ObjectRestResponse <br>
	 * @throws BaseException <br>
	 * */
	@ApiOperation(value = "匿名登录", notes = "匿名登录")
	@PostMapping(value = "/anonymous")
	@ApiImplicitParam(name = "param", value = "第三方登录第二步param", required = true, dataType = "UserTermDeviceEntity")
	public ObjectRestResponse<Object> anonymous(@RequestBody UserTermDeviceEntity param) {
		log.info("[用户注册]-[匿名登录] start...");
		
		ObjectRestResponse<Object> objectRestResp = new ObjectRestResponse<Object>();
		String imei = param.getMobileImei();
		Integer userType = param.getUserType();
		if (StringUtils.isBlank(imei)) {
			objectRestResp.setStatus(UserConstant.USER_PHONE_50710);
			objectRestResp.setMessage(UserConstant.USER_PHONE_50710_MSG);
			objectRestResp.setRel(false);
			return objectRestResp;
		}
		
		//判断用户类型不能为空
		if(userType ==null){
			objectRestResp.setStatus(UserConstant.USER_TYPE_ERROR_CODE);
			objectRestResp.setMessage(UserConstant.USER_TYPE_ERROR_MSG);
			objectRestResp.setRel(false);
			return objectRestResp;
		}
		AnonymousRsp anonymousRsp = new AnonymousRsp();
		try {
			AnonymousInfo anonymousInfo = userService.loginByTerm(param);
			//封装返回参数
			anonymousRsp.setNickName(anonymousInfo.getNickName());
			anonymousRsp.setUserId(DESEncrypt.encryptMode(desKey,anonymousInfo.getId().toString()));
			anonymousRsp.setUserType(userType);
			anonymousRsp.setAnonymous(anonymousInfo.getAnonymous());
			anonymousRsp.setUsername(anonymousInfo.getUsername());
			//注册返回token
			String token = jwtTokenUtil.generateToken(new JWTInfo(anonymousInfo.getUsername(), 
						anonymousInfo.getId() + "", "",userType+"", ""));
			
			anonymousRsp.setToken(token);
		} catch (Exception e) {
			log.error("[用户注册]-[第三方登录第二步]--第三方登录第二步失败！");
			throw new BaseException("[用户注册]-[第三方登录第二步]--数据插入失败-异常，错误message:"+e.getMessage(),
					RestCodeConstants.THROW_ERROR_CODE);
		}
		
		objectRestResp.setRel(true);
		objectRestResp.setStatus(RestCodeConstants.SUCCESS_CODE);
		objectRestResp.setMessage("[用户注册]-[匿名登录].success!");
		objectRestResp.setData(anonymousRsp);
		return objectRestResp;
	}
	
	/**
	 * Description:手机号+密码登录 <br>
	 * @author stf<br>
	 * @param LoginVo <br>
	 * @return ObjectRestResponse <br>
	 * @throws BaseException <br>
	 * */
	@ApiOperation(value = "手机号+密码登录", notes = "手机号+密码登录")
	@PostMapping("/login_by_phoneAndpasswd")
	@ApiImplicitParam(name = "param", value = "手机号+密码登录param", required = true, dataType = "LoginVo")
	public ObjectRestResponse<Object> loginByCellphoneNumberAndPassword(@RequestBody LoginVo param) {
		log.info("[用户注册]-[手机号+密码登录] start...");
		ObjectRestResponse<Object> objectRestResp = new ObjectRestResponse<Object>();
		
		String mobile = param.getMobile();
		String passwd = param.getPasswd();
		Integer userType = param.getUserType();
		
		//验证参数
		if (StringUtils.isAnyBlank(mobile, passwd) || null == userType) {
			log.error("[用户注册]-[手机号+密码登录]--参数为空！");
			objectRestResp.setStatus(UserConstant.USER_PHONE_REGISTER_CODE);
			objectRestResp.setMessage(UserConstant.USER_PHONE_REGISTER_MSG);
			objectRestResp.setRel(false);
			return objectRestResp;
		}
		
		//手机号码格式校验
		if(!Pattern.matches(ServerConstants.REGMOBILE, mobile)) {
			log.error("[用户注册]-[手机号+密码登录]--电话号码不正确");
			objectRestResp.setStatus(UserConstant.USER_PHONE_REGISTER_50710);
			objectRestResp.setMessage(UserConstant.USER_PHONE_REGISTER_50710_MSG);
			objectRestResp.setRel(false);
			return objectRestResp;
		}
		
		TbAuUser auUser = new TbAuUser();
		
		//添加 一天登录输入错误密码5次验证
		String errNum =new String("");
		int errNums = 0;
		try {
			Object redisVcodeErrNum =redisCacheUtil.getCache(RedisConstants.REDIS_VCODE_PREFIX+RedisConstants.KEY_SPLIT+mobile);
			if(null != redisVcodeErrNum){
				errNum = redisVcodeErrNum.toString();
			}
			if(errNum !=null && !errNum.equals("")){
				errNums = Integer.parseInt(errNum);
			}
			if(errNums>5){
				//登录错误次数大于5次，今天不允许登录了
				objectRestResp.setStatus(UserConstant.USER_60100);
				objectRestResp.setMessage(UserConstant.USER_60100_MSG);
				objectRestResp.setRel(false);
				return objectRestResp;
			}
		} catch (Exception e) {
			log.error("[用户注册]-[手机号+密码登录]--系统内部错误！");
			throw new BaseException("[用户注册]-[手机号+密码登录]--系统内部错误，错误message:"+e.getMessage(),
					RestCodeConstants.SYSTEM_ERROR_CODE);
		}
		
		//手机号+密码登录验证
		try {
			auUser = userService.loginByPhoneAndPassword(param);
			if(null !=auUser){
				//校验userType
				Map<String,Object> reqMap =new HashMap<String,Object>();
				reqMap.put("userId", auUser.getId());
				reqMap.put("userTypeId", param.getUserType());
				if(!userService.veritfyUserType(reqMap)){
					objectRestResp.setStatus(UserConstant.USER_PHONE_50810);
					objectRestResp.setMessage(UserConstant.USER_PHONE_50810_MSG);
					objectRestResp.setRel(false);
					return objectRestResp;
				}
			}
		} catch (Exception e) {
			objectRestResp.setStatus(UserConstant.USER_PHONE_50810);
			objectRestResp.setMessage(UserConstant.USER_PHONE_50810_MSG);
			objectRestResp.setRel(false);
			return objectRestResp;
		}
		
		try{
			
			PhoneUserLoginRsp phoneUserLoginRsp = new PhoneUserLoginRsp();
			BeanUtils.copyProperties(auUser, phoneUserLoginRsp);
			phoneUserLoginRsp.setUserId(DESEncrypt.encryptMode(desKey,auUser.getId().toString()));
			if(auUser.getParentId() != null){
				phoneUserLoginRsp.setParentId(DESEncrypt.encryptMode(desKey,auUser.getParentId().toString()));
			}
			phoneUserLoginRsp.setUserType(param.getUserType());
			//注册返回token
			String token = jwtTokenUtil.generateToken(new JWTInfo(auUser.getUsername(), auUser.getId() + "", "",param.getUserType()+"", auUser.getTelPhone()));
			phoneUserLoginRsp.setToken(token);
			
			//登录成功重置错误次数
			redisCacheUtil.putCacheWithExpireTime(RedisConstants.REDIS_VCODE_PREFIX+RedisConstants.KEY_SPLIT+mobile, 
					RedisConstants.REDIS_RIGHT_COUNT, RedisConstants.REDIS_VERIFTY_DEFALUT_EXPIRETIME);
			
			objectRestResp.setRel(true);
			objectRestResp.setStatus(RestCodeConstants.SUCCESS_CODE);
			objectRestResp.setMessage("[用户注册]-[手机号+密码登录].success!");
			objectRestResp.setData(phoneUserLoginRsp);
			return objectRestResp;
		} catch (Exception e) {
			errNums = errNums+1;
			try {
				redisCacheUtil.putCacheWithExpireTime(RedisConstants.REDIS_VCODE_PREFIX+RedisConstants.KEY_SPLIT+mobile, 
						errNums, RedisConstants.REDIS_VERIFTY_DEFALUT_EXPIRETIME);
			} catch (Exception e1) {
				log.error("[用户注册]-[手机号+密码登录]--redis异常！");
				throw new BaseException("[用户注册]-[手机号+密码登录]--系统内部错误，错误message:"+e.getMessage(),
						RestCodeConstants.SYSTEM_ERROR_CODE);
			}
			objectRestResp.setStatus(UserConstant.USER_60110);
			objectRestResp.setMessage(UserConstant.USER_60110_MSG);
			objectRestResp.setRel(false);
			return objectRestResp;
			
		}
		
	}

	
	/**
	 * Description:重置密码 <br>
	 * @author stf<br>
	 * @param LoginVo <br>
	 * @return ObjectRestResponse <br>
	 * @throws BaseException <br>
	 * */
	@ApiOperation(value = "重置密码", notes = "重置密码（或忘记密码找回操作）")
	@PostMapping("/resetPasswdByPhone")
	@ApiImplicitParam(name = "param", value = "重置密码param", required = true, dataType = "Manager4Password")
	public ObjectRestResponse<Object> resetPasswdByPhone(@RequestBody Manager4Password param){
		ObjectRestResponse<Object> objectRestResp = new ObjectRestResponse<Object>();
		String mobile = param.getMobile();
		String password = param.getPasswd();
		String smsCode = param.getSmsCode();
		if (StringUtils.isAnyBlank(mobile, password,smsCode)) {
			log.error("[重置密码]--参数为空！");
			objectRestResp.setStatus(UserConstant.USER_PHONE_REGISTER_CODE);
			objectRestResp.setMessage(UserConstant.USER_PHONE_REGISTER_MSG);
			objectRestResp.setRel(false);
			return objectRestResp;
			
		}
		
		try {
			boolean isVerified = verificationCodeService.verifyVerificationCode(mobile, smsCode);
			if (!isVerified) {
				log.error("[重置密码]-短信验证码不正确或已超时!");
				objectRestResp.setStatus(UserConstant.USER_PHONE_50720);
				objectRestResp.setMessage(UserConstant.USER_PHONE_50720_MSG);
				objectRestResp.setRel(false);
				return objectRestResp;
			}
			
			userService.resetPwdByPhoneNum(mobile, password);
		} catch (Exception e) {
			log.error("[重置密码]-系统错误，错误msg:"+e.getMessage());
			objectRestResp.setStatus(RestCodeConstants.SYSTEM_ERROR_CODE);
			objectRestResp.setMessage("[重置密码]-系统错误，错误msg:"+e.getMessage());
			objectRestResp.setRel(false);
			return objectRestResp;
		}
		
		objectRestResp.setStatus(RestCodeConstants.SUCCESS_CODE);
		objectRestResp.setMessage("密码重置成功，请重新登录！");
		objectRestResp.setData(true);
		objectRestResp.setRel(true);
		return objectRestResp;
	}

}
